#ifndef GAME_H
#define GAME_H

#include <vector>
#include "map.h"
#include "npc.h"
#include "player.h"
#include "animation.h"
#include "decisiontree.h"

using namespace std;

class game{
	
	public:
		game(player* dude);
		~game();
		
		decisionTree* decis;
		animation* ani;
		vector <map *> gameMaps;
		vector <npc *> gameNpcs;
		ALLEGRO_BITMAP* gameTitle;
		enum STATES {TITLE, PLAYING};
		int state;
		player* dude;
		bool playerTurn;
		
		void putMapIntoVector(map* map1);
		void putNpcIntoVector(npc* npc1);
		void drawScene();
		void drawNpc();
		void drawTitle();
		void addAni(animation* ani);
		void npcControl();
		void addDecision(decisionTree* decis);
		void changeStateTitle();
		void initTitle();
		void killTitle();
		void changeStatePlaying();
	private:
};

//Contructor: pass in the player; the first state should be the title 
game::game(player* dude){
	this->dude=dude;
	changeStateTitle();
}

//Destructor
game::~game(){
}

//adds a map pointer to a vector of maps
void game::putMapIntoVector(map* map1){
	gameMaps.push_back(map1);
}

//puts a non playable character pointer to a vector of non playable characters
void game::putNpcIntoVector(npc* npc1){
	gameNpcs.push_back(npc1);
}

//draw title to screen if the state is equal to title
//if not the map non playable characters and the players character
//will be drawn
void game::drawScene(){
	if(state==TITLE){
		drawTitle();
	}else{
	gameMaps[0]->drawMapBackground();
	drawNpc();
	al_draw_bitmap(dude->playerImage,dude->x,dude->y,0);
}
}

//draw each non playable character if it is live
void game::drawNpc(){
	int size=gameNpcs.size();
	for(int i = 0; i <size; i++){
		gameNpcs[i]->dead();
		if(gameNpcs[i]->live)
		al_draw_bitmap(ani->characterGraphics[gameNpcs[i]->graphicId],gameNpcs[i]->x,gameNpcs[i]->y,0);
	}
}

//adds the animation object pointer
void game::addAni(animation* ani){
	this->ani=ani;
}

//adds the decision tree pointer
void game::addDecision(decisionTree* decis){
	this->decis=decis;
}

//calls makeDecision function fron the decision tree for each non playable
//character
void game::npcControl(){
	if(state==PLAYING){
	int size = gameNpcs.size();
	for(int i = 0; i< size; i++){
		if(gameNpcs[i]->live==true)
		decis->makeDecision(gameNpcs[i]);
	}
	}
}

//draws the title 
void game::drawTitle(){
	al_draw_bitmap(gameTitle,gameMaps[0]->camera_x,gameMaps[0]->camera_y,0);
}

//initialize the title image 
void game::initTitle(){
	ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
	al_set_path_filename(path, "images/maxvswordtitle.png");
	gameTitle=al_load_bitmap(al_path_cstr(path, '/'));
}

//destroys the title image
void game::killTitle(){
	al_destroy_bitmap(gameTitle);
}

//changes the state to the title state
void game::changeStateTitle(){
	state=TITLE;
	initTitle();
}

//changes the state to playing
void game::changeStatePlaying(){
	state=PLAYING;
	killTitle();
}
#endif
